<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class ListController extends CUserController {
    public $layout = '//layouts/list';
    public $cataName = null;
    public $isDataSource = true;
    public $aid = null;
    
    
    public function actionIndex() {
        $aid = Yii::app()->request->getParam('aid', '1');
        $ct = Yii::app()->request->getParam('ct', 'file');
        $ac = Yii::app()->request->getParam('ac', 'userfile');
        $user = Yii::app()->session["user"];
        $user_id = $user["id"];
        
                $this->cataName = $this->getCataName($aid);
        $this->aid = $aid;
        
        if($ct == "file" && $ac == "userfile") {
            if($aid == 1) {                 $this->listFile($user_id);
            }elseif($aid == 2) {                 $this->document($user_id);
            }elseif($aid == 3) {                 $this->album($user_id);
            }elseif($aid == 4) {                 $this->music($user_id);
            }elseif($aid == 9) {                 $this->video($user_id);
            }elseif($aid == 7) {                 $this->layout = '';
                $this->trash($user_id);
            }elseif($aid == 32) {                 $this->layout = '/layouts/analyze';
                $this->newUpload($user_id);
            }elseif($aid == 30) {                 $this->layout = '/layouts/analyze';
                $this->myShare($user_id);
            }elseif($aid == 31) {                 $this->layout = '/layouts/analyze';
                $this->myLove($user_id);
            }elseif($aid == 35) {                 $this->location($user_id, $aid);
            }elseif($aid == -1) {
                $this->redirect(array("netdisk/homepage"));
            }
        }elseif($ct == "file" && $ac == "search") {             $this->search($user_id);
        }else {
            $this->listFile($user_id);
        }
    }
    
    
    private function search($user_id) {
        
        $aid = Yii::app()->request->getParam('aid', '1');
        $start = Yii::app()->request->getParam('s', '0');
        $date = Yii::app()->request->getParam('date', '0');
        
                $limit = 30;
        $searchValue = Yii::app()->request->getParam('search_value', '1');
        
        $fileList = new FileList();
        
        $time = "";
        if($date == "1") {
            $time = date("Y-m-d", strtotime("-1 day"));
        }else if($date == "3") {
            $time = date("Y-m-d", strtotime("-3 day"));
        }else if($date == "7") {
            $time = date("Y-m-d", strtotime("-1 week"));
        }else if($date == "30") {
            $time = date("Y-m-d", strtotime("last month"));
        }
        
        $condition = "user_id=:user_id and is_deleted = 0 and file_type = 0 and file_name like :file_name ";
        $params = array();
        $params[':user_id'] = $user_id;
        $params[':file_name'] = "%" . $searchValue . "%";
        switch($aid) {
            case 2 :
                $tpyes = Yii::app()->params['officeType'];
                $mime_type = "'" . implode("','", array_values($tpyes)) . "'";                 $condition .= "and version_id in(";
                $condition .= "select id from " . Yii::app()->params['tablePrefix'] . "file_versions where mime_type in ($mime_type))";
                break;
            case 3 :
                $condition .= "and version_id in(";
                $condition .= "select id from " . Yii::app()->params['tablePrefix'] . "file_versions where mime_type like 'image%')";
                break;
            case 4 :
                $condition .= "and version_id in(";
                $condition .= "select id from " . Yii::app()->params['tablePrefix'] . "file_versions where mime_type like 'audio%')";
                break;
            case 5 :
                $condition .= "and version_id in(";
                $condition .= "select id from " . Yii::app()->params['tablePrefix'] . "file_versions where mime_type like 'video%')";
                break;
            default :
                break;
        }
        
                if($date != 0) {
            $condition .= " and updated_at>=:updated_at";
            $params[":updated_at"] = $time;
        }
        
        $list = $fileList->search($condition, $params, $limit, $start);
        
        $count = UserFile::model()->count($condition, $params);
        $pageSize = ceil($count / $limit);
        $currentPage = $start / $limit + 1;
        
        $this->render("search", array("list" => $list, "pageSize" => $pageSize, "limit" => $limit, "currentPage" => $currentPage, "aid" => $aid, "searchValue" => $searchValue, "count" => $count));
    }
    
    
    private function myShare($user_id) {
        $this->layout = '/layouts/analyze';
        $this->render("myshare");
    }
    
    
    private function myLove($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        
        $fileHandle = new MyloveList();
        $list = $fileHandle->obtainMyloveList($user_id, $limit, $start);
        $count = $fileHandle->myloveCount($user_id);
        $currentPage = $start / $limit + 1;
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    
    private function trash($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        
        $fileHandle = new TrashList();
        $count = $fileHandle->trashCount($user_id);
        $list = $fileHandle->obtainTrashList($user_id, $limit, $start);
        $currentPage = $start / $limit + 1;
        $pageSize = ceil($count / $limit);
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("trash", array("list" => $list, "aid" => $aid, "pageSize" => $pageSize, "limit" => $limit, "currentPage" => $currentPage, "count" => $count));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    
    private function newUpload($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 20);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        
        $currentPage = $start / $limit + 1;
        $fileHandle = new NewUploadList();
        $limit = 20;         $list = $fileHandle->obtainNewUploadList($user_id, $limit);
        $count = count($list);
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    
    private function listFile($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        if($cid == -1) {
            $cid = 0;
        }
        $currentPage = $start / $limit + 1;
        
        $fileHandle = new FileList();
        
        $list = $fileHandle->obtainFileList($user_id, $cid, $limit, $start);
        $count = $fileHandle->fileCount($user_id, $cid);
        $nav = $fileHandle->navigation($user_id, $cid);
        $pageSize = ceil($count / $limit);
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render($tpl, array("list" => $list, "pageSize" => $pageSize, "nav" => $nav, "limit" => $limit, "cid" => $cid, "currentPage" => $currentPage, "count" => $count));
        }else {
            $this->redirect("/static/html/error.html");
        }
    
    }
    
    
    private function document($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        
        $currentPage = $start / $limit + 1;
        $fileHandle = new DocumentList();
        $list = $fileHandle->obtainDocumentList($user_id, $limit, $start);
        $count = $fileHandle->documentCount($user_id);
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    
    private function album($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', -1);
        $aid = Yii::app()->request->getParam('aid', 0);
        $currentPage = $start / $limit + 1;
        $fileHandle = new AlbumList();
        $list = $fileHandle->obtainAlbumList($user_id, $limit, $start, $cid);
        $nav = $fileHandle->navigation($user_id, $cid);
        $count = $fileHandle->albumCount($user_id, $cid);
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text, "nav" => $nav));
        }else {
            $this->redirect("/static/error.html");
        }
    
    }
    
    
    private function music($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        
        $currentPage = $start / $limit + 1;
        $fileHandle = new MusicList();
        $list = $fileHandle->obtainMusicList($user_id, $limit, $start);
        $count = $fileHandle->musicCount($user_id);
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    private function video($user_id) {
        $tpl = Yii::app()->request->getParam('tpl', 'list_pg');
        $limit = Yii::app()->request->getParam('limit', 45);
        $start = Yii::app()->request->getParam('s', 0);
        $cid = Yii::app()->request->getParam('cid', 0);
        $aid = Yii::app()->request->getParam('aid', 0);
        
        $currentPage = $start / $limit + 1;
        $fileHandle = new VideoList();
        $list = $fileHandle->obtainVideoList($user_id, $limit, $start);
        $count = $fileHandle->videoCount($user_id);
        $pageSize = ceil($count / $limit);
        $text = $this->getCataName($aid);
        $test = $text['prompt'];
        
        if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
            $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text));
        }else {
            $this->redirect("/static/error.html");
        }
    }
    
    private function location($user_id, $aid) {
                                $flag = Yii::app()->request->getParam('flag', 0);
        
        if($flag < 0) {
            
            $tpl = Yii::app()->request->getParam('tpl', 'view_pg');
            $ids = Yii::app()->request->getParam('ids');
            $loc = Yii::app()->request->getParam('loc', '未知位置');
            $limit = Yii::app()->request->getParam('limit', 45);
            $start = Yii::app()->request->getParam('s', 0);
            $aid = 35;
            $cid = 0;
            $currentPage = $start / $limit + 1;
            $fileHandle = new AlbumList();
            $list = $fileHandle->obtainAlbumListByLocation($user_id, $limit, $start, $ids);
                        $nav = Array();
            $data = Array();
            $data["id"] = -2;
            $data["fileName"] = $loc;
            $data["parent_file_id"] = -2;
            array_push($nav, $data);
            
            $count = count(explode(",", $ids));
            $pageSize = ceil($count / $limit);
            $text = $this->getCataName($aid);
            
            if($tpl == "list_pg" || $tpl == "view_pg" || $tpl == "view_large_pg") {
                $this->render("multi_" . $tpl, array("list" => $list, "pageSize" => $pageSize, "currentPage" => $currentPage, "limit" => $limit, "cid" => $cid, "aid" => $aid, "count" => $count, "basic" => $text, "nav" => $nav, "loc" => $loc, "ids" => $ids));
            }
        }else {
            
            $photoGps = Yii::app()->request->getParam('gps');
            $tid = Yii::app()->request->getParam('tid');
            $tpl = Yii::app()->request->getParam('tpl', 'view_pg');
            $vid = Yii::app()->request->getParam('version');
            $cid = Yii::app()->request->getParam('cid');
            $name = Yii::app()->request->getParam('name');
            
            $nav = Array();
            if(isset($name)) {
                $data = Array();
                $data["id"] = -2;
                $data["fileName"] = $name;
                $data["parent_file_id"] = -2;
                array_push($nav, $data);
            }
            $text = $this->getCataName($aid);
            $this->render('multi_map_pg', array('flag' => $flag, 'tpl' => $tpl, 'aid' => $aid, 'cid' => $cid, 'basic' => $text, 'gps' => $photoGps, 'tid' => $tid, 'vid' => $vid, "nav" => $nav, 'name' => $name));
        }
    
    }
    
    private function getCataName($aid) {
        $array = array();
        if($aid == 1) {
            $array['cataName'] = "我的文件";
            $array['titleName'] = "双击下载文件";
            $array['type'] = "file";
        }elseif($aid == 2) {
            $array['cataName'] = "我的文档";
            $array['titleName'] = "双击查看文档";
                        $array['type'] = "file";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前目录没有文档</h3><p>您在\"我的文档\"可以永久存储诸多办公类文档！</p><p>每天工作存档、随时在线办公;\"我的文档\"让工作简单、有序！</p><p>办公类文档的类型主要包括常用的word、excel、ppt、pdf、wps、txt等等！</p></div></div>";
        }elseif($aid == 3) {
            $array['cataName'] = "我的相册";
            $array['titleName'] = "双击查看图片";
            $array['type'] = "photo";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前相册没有照片</h3><p>您在\"我的相册\"中可以永久存储诸多图片！</p><p>色彩丰富生活，留影无处不在，回忆甜蜜瞬间!</p></div></div>";
        }elseif($aid == 4) {
            $array['cataName'] = "我的音乐";
            $array['titleName'] = "双击播放音乐";
            $array['type'] = "music";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前专辑没有音乐</h3><p>您在\"我的音乐\"中可以永久存储诸多音乐！</p><p>美妙音乐，在线聆听！无论你在哪里，无论你用的是电脑、是手机、还是ipad；随时随地，轻轻哼哼，一身激情！</p></div></div>";
        }elseif($aid == 9) {
            $array['cataName'] = "我的视频";
            $array['titleName'] = "双击播放视频";
            $array['type'] = "file";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前目录没有视频文件</h3><p>您在\"我的视频\"分类中可以永久存储属于您的视频文件！</p><p>支持所有视频格式文件上传！</p></div></div>";
        }elseif($aid == 30) {
            $array['cataName'] = "我的分享";
            $array['cataName'] = "我的分享";
            $array['titleName'] = "";
            $array['type'] = "file";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前目录没有分享</h3><p>您在\"我的分享\"分类中可以查看分享的文件！</p></div></div>";
        }elseif($aid == 31) {
            $array['cataName'] = "我的最爱";
            $array['titleName'] = "";
            $array['type'] = "file";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前目录没有最爱文件</h3><p>您在\"我的最爱\"分类中可以查看加星文件！</p></div></div>";
        }elseif($aid == 32) {
            $array['cataName'] = "最新上传";
            $array['titleName'] = "";
            $array['type'] = "file";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前目录没有文件</h3><p>您在\"最新上传\"分类中可以查看最新上传的文件！</p></div></div>";
        }elseif($aid == 35) {
            $array['cataName'] = "地理";
            $array['titleName'] = "";
            $array['type'] = "photo";
            $array['prompt'] = "<h3><i class=\"i-hint ico-inf\"></i>当前相册没有含有GPS的照片</h3><p>您在\"我的相册\"中可以永久存储诸多图片！</p><p>色彩丰富生活，留影无处不在，回忆甜蜜瞬间!</p></div></div>";
            $array['important'] = "<h3><i class=\"i-hint ico-inf\"></i>对不起！您没有打开php_exif扩展,图片gps信息将无法读取</h3></div></div>";
        }elseif($aid == 7) {
                                                                    }
        return $array;
    }
    
    public function actionMapalbum() {
        $flag = Yii::app()->request->getParam('flag', 0);
                                $this->isDataSource = false;
        
        if($flag == 0) {
            
                                                $text = $this->getCataName(35);
            $fileHandle = new AlbumList();
                                                $flag = -1;
            if ($fileHandle->submitGpsinfos()) {
                $flag=$fileHandle->isHaveGps();
            }
                        $is_mini=Yii::app()->params['app']['eversion'];
            $this->render('map',array('flag' => $flag, 'basic' => $text,'is_mini'=>$is_mini));
            
        }else {
            
            $aid      = 35;
            $photoGps = Yii::app()->request->getParam('gps');
            $tid      = Yii::app()->request->getParam('tid');
            $tpl      = Yii::app()->request->getParam('tpl', 'view_pg');
            $vid      = Yii::app()->request->getParam('version');
            $cid      = Yii::app()->request->getParam('cid');
            $name     = Yii::app()->request->getParam('name');
            $text     = $this->getCataName($aid);
            
            $this->render('get_view_map', array('flag' => $flag, 'tpl' => $tpl, 'aid' => $aid, 'cid' => $cid, 'basic' => $text, 'gps' => $photoGps, 'tid' => $tid, 'vid' => $vid, 'name' => $name));
        }
    }

}